package com.example.wastes.shopping.controller;

import com.example.wastes.common.pojo.Shopping;
import com.example.wastes.common.pojo.User;
import com.example.wastes.common.util.R;
import com.example.wastes.shopping.mapper.ShoppingMapper;
import com.example.wastes.shopping.service.ShoppingService;
import com.example.wastes.user.mapper.UserMapper;
import com.example.wastes.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
public class ShoppingController {
    @Autowired
    private ShoppingService shoppingService;
    @Autowired
    private UserService userService;
    @GetMapping("/getAllShopping")
    @ApiOperation("查询所有商品")
    public R getAllShopping(){
        return new R(true,shoppingService.list());
    }
    @PostMapping("/saveShopping")
    @ApiOperation("添加商品")
    public R saveShopping(@RequestBody Shopping shopping){
        return new R(shoppingService.save(shopping));
    }
    @PutMapping("/updateShopping")
    @ApiOperation("修改商品")
    public R updateShopping(@RequestBody Shopping shopping){
        return new R(shoppingService.updateById(shopping));
    }
    @DeleteMapping("/deleteShopping")
    @ApiOperation("删除商品")
    public R deleteShopping(Integer id){
        return new R(shoppingService.removeById(id));
    }
    @GetMapping("/getShoppingranking")
    @ApiOperation("通过积分排序商品")
    public R getShoppingranking(){
        return new R(true,shoppingService.findShoppingranking());
    }
    @GetMapping("/findShopingcondition")
    @ApiOperation("通过积分排序商品")
    public R findShopingcondition(int id){
        return new R(true,shoppingService.findShopingcondition(id));
    }
    @PostMapping("/upload")
    @ApiOperation("上传商品图片")
    public String upload(Shopping shopping, MultipartFile filename, HttpServletRequest request){
        int i = shoppingService.upload(shopping,filename,request);
        if (i>0){
            return "上传成功";
        }else{
            return "上传失败";
        }
    }

    @GetMapping("/compare")
    @ApiOperation("通过用户的积分兑换所需要的商品")
    public Object compare(@RequestParam(value = "id") Integer id,@RequestParam(value = "jifen") Integer jifen){
        User user = userService.SelectIntegral(id);
        int integral=user.getIntegral();
        Shopping shopping = shoppingService.SelectIntegral(jifen);
        int j =shopping.getIntegral();
        System.out.println(integral);
        System.out.println(j);
        if (integral>=j){
            int update = integral -j;
            User user1 = new User();
            user1.setId(id);
            user1.setIntegral(update);
            userService.updateById(user1);
            return "兑换成功";

        }else {
            return "积分不足";
        }
    }
    //模糊查询
    @GetMapping("findShoppingName")
    public R findShoppingName(String shoppingname){
        return new R(true,shoppingService.findShoppingName(shoppingname));
    }

    @PostMapping("/shoppingImg")
    @ApiOperation("上传商品图片")
    public String shoppingImg(Shopping shopping, @RequestParam MultipartFile filename, HttpServletRequest request){
        int i = shoppingService.shoppingImg(shopping,filename,request);
        if (i>0){
            return "上传成功";
        }else{
            return "上传失败";
        }
    }
}
